<?php

// +----------------------------------------------------------------------
// | 分类控制器
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace app\controller\app\admin;

use yicmf\builder\Edit;
use yicmf\builder\Table;
use think\facade\Url;
use app\controller\admin\Common;
use app\model\app\AppTaskNew as AppTaskNewModel;
use app\event\app\ThirdRecharge as ThirdRechargeEvent;


class AppTaskNew extends Common
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function _filter()
    {
    }

    public function index()
    {

        $statusOptions = [
            [
                'id' => 1,
                'value' => '信用卡'
            ],
            [
                'id' => 2,
                'value' => '贷款'
            ],
            [
                'id' => 3,
                'value' => '租机'
            ]
        ];
        //读取规则列表
        $map = $this->_search($this->dbname);
        //            //显示页面
        $builder = new Table();
        $builder->model(AppTaskNewModel::class)
            ->where($map)
            ->order('id DESC')
//            ->searchSelect('type','产品类型',$statusOptions)
//            ->searchBool('is_new','新品')
            ->searchBool('is_online', '是否开放')
            ->keyId()
            ->keyText('name', '产品名称')
            ->keyImage('image', '封面')
            ->keyBool('is_online', '是否开放')
//            ->keyUser('user_id', '用户')
            ->keyUpdateTime()
//                ->keyText('applyMsg','简介')
//            ->keyText('sort', '排序')

            ->actionUpdate();
        return $builder->fetch();
    }

    /**
     * 编辑分类
     * @access protected
     * @author 微尘 <yicmf@qq.com>
     */
    public function update()
    {
        if ($this->request->isPost()) { //提交表单
            $check = $this->validate($this->request->param(), \app\validate\app\AppTask::class);
            if ($check !== true) {

                $this->_returnError($check);
            } else {
                if ($this->request->param('id')) {

                    $data = AppTaskNewModel::where('id', $this->request->param('id'))->find();
                    $info = $this->request->except(['id']);
                    $data->save($info);
                    if ($task = TaskModel::where('type', 6)->where('relation_id', $data['id'])->find()) {
                        // 更新
                        $task['title'] = $data['bankName'];
                        $task['commission'] = $data['commission'];
                        $task['introduce'] = $data['shortIntroduce'];
                        $task->save();
                    } else {
                        $task['name'] = '信用产品';
                        $task['title'] = $data['bankName'];
                        $task['commission'] = $data['commission'];
                        $task['is_top'] = $data['is_hot'];
                        $task['introduce'] = $data['shortIntroduce'];
                        $task['relation_id'] = $data['id'];
                        $task['user_id'] = $this->user['id'];
                        $task['type'] = 6;
                        $re = TaskModel::create($task);
                    }
                    $this->_returnSuccess('更新成功');
                } else {
                    $re = AppTaskNewModel::create($this->request->param());
                    if ($re) {
                        // 新增任务
                        $task['name'] = '信用产品';
                        $task['title'] = $re['bankName'];
                        $task['introduce'] = $re['shortIntroduce'];
                        $task['commission'] = $re['commission'];
                        $task['is_top'] = $re['is_hot'];
                        $task['relation_id'] = $re['id'];
                        $task['user_id'] = $this->user['id'];
                        $task['type'] = 6;
                        $re = TaskModel::create($task);
                        if ($re) {
                            $this->_returnSuccess('新增成功');
                        } else {
                            $this->_returnError('新增异常');
                        }
                    } else {

                        $this->_returnError('新增异常');
                    }
                }
            }

        } else {
            /* 获取分类信息 */
            $id = $this->request->get('id');
            $isEdit = $id ? true : false;
            if ($isEdit) {
                $data = AppTaskNewModel::where('id', $id)->find();
                if (!$data) {
                    $this->_returnError('分类不存在或者已经删除');
                }
            } else {
                $data['type'] = 1;
            }
            $builder = new Edit();
            $builder->title('编辑')
                ->keyText('name', '任务名称')
//                ->keyText('product_url', '渠道链接')
                ->keyImage('image', '封面', '')
                ->keyEditor('rw_notice', '弹出公告')
                ->keyEditor('app_yq', '任务要求')
                ->keyEditor('jsbz_text', '结算标准')
                ->keyEditor('tgm_text', '开通权限')
                ->keyEditor('rw_jc', '图文教程');

            $builder->keyBool('is_online', '是否上架')
                ->data($data)
                ->buttonSubmit('', '保存');
            //                    ->buttonClose();
            return $builder->fetch();
        }
    }
}